
let Base = require('Base');
var axios = require("axios");
cc.macro.ENABLE_TRANSPARENT_CANVAS = true;
cc.Class({
    extends: Base,
    ctor(){
        //游戏状态：ready, start, over
        this.gameState = 'ready';
        //游戏进行时间
        this._startTime = 0;
        //帧速率 60帧/s
        this.FPS = 60;
    },
    properties: {
        //是否是倒计时
        isCountDown: {
            type: Boolean,
            default: false,
        },
        //游戏时间（倒计时）
        gameTime: {
            type: Number,
            default: 30,
        },
        //分数父节点
        scoreBox: cc.Node,
        //减分气泡
        pubb: cc.Prefab,
        //加分气泡
        pubb2: cc.Prefab,      
        //游戏时间
        startTime: {
            get: function(){
                return this._startTime
            },
            set: function(val){
                this._startTime = val;
                //倒计时
                if(this.isCountDown){
                    console.log(this.gameTime - this._startTime)
                    this.commTopNode.emit('updateNum', {                        
                        time: Math.floor(this.gameTime - this._startTime)
                    })  
                //正计时  
                }else{
                    this.commTopNode.emit('updateNum', {
                        time: Math.floor(this._startTime)
                    })
                }
            }
        }
    },
    _onLoad () {
        //获取节点
        this._initNode()
        //绑定事件
        this._initBindEvent()
    },
    /*
        生成分数气泡
        参数：
            curPos: [cc.Node, cc.Vec2]
            num: Number
    */
    createScoreNode(curPos, num){
        if(curPos instanceof cc.Node){
            curPos = curPos.convertToWorldSpaceAR(cc.v2(0,0))
        }
        let pubb = cc.instantiate(num<0?this.pubb:this.pubb2);
        let label = pubb.children[0].getComponent(cc.Label);
        label.string = num>0?'+'+num:String(num);
        let disPos = curPos.sub(this.scoreBox.convertToWorldSpaceAR(cc.v2(0,0)));
        pubb.setPosition(disPos)
        this.scoreBox.addChild(pubb)
        cc.tween(pubb).bezierTo(2, cc.v2(disPos.x, disPos.y*2/3), cc.v2(0, disPos.y*1/3), cc.v2(0,0)).call(()=>{
            pubb.destroy();
        }).start()
    },    
    //开始游戏
    _startGame(){
        this.gameState = 'start';
        this.gameStartNode.active = false;
        this._resetGame();
        this.startGame && this.startGame();        
    },
    //重置游戏
    _resetGame(){
        this.startTime = 0;
        this.score = 0;
        this.resetGame && this.resetGame();
    },
    //游戏结束
    _gameOver(){
        this.gameState = 'over';
        /* 游戏结束提交数据到后台 */
        /*
            axios().then(()=>{
                this.submitCallBack();
            })
        */

        this.gameOver && this.gameOver();
    },
    //游戏结束，从外面调用
    callGameOver(){
        this._gameOver();
    },   
    //绑定事件
    _initBindEvent(){
        //开始游戏
        this.gameStartNode.on('touchstart', (event)=>{
            this._startGame();
            event.stopPropagation()
        })
    },    
    //获取节点
    _initNode(){
        this.canvas = cc.find('Canvas');
        //游戏主场景 节点
        this.gameBox = cc.find('Canvas/gameBox');
        //公用计分组件
        this.commTopNode = cc.find('Canvas/gameBox/commTop');
        //游戏结束场景 节点
        //this.gameAfterNode = cc.find('Canvas/gameOverLayer');
        //开始游戏
        this.gameStartNode = cc.find('Canvas/startGame');
    }, 
    _update (dt) {
        this.FPS = Math.floor(1/dt);

        if(this.gameState != 'start'){
            return false;
        }
        this.startTime+=dt;
        //倒计时
        if(this.isCountDown){
            if(this.startTime >= this.gameTime){
                //this.startTime = 0;
                this._gameOver();
            }     
        }
    },
});
